package com.jc.oms.controller.menu;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.CheckNullUtils;
import com.jc.common.util.HttpUtil;
import com.jc.common.util.WebUtils;
import com.jc.ds.model.Permission;
import com.jc.service.menu.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/menuList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> menuList() {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String, Object>> maps = menuService.queryMenuList();
        result.put("code", 10000);
        result.put("data", maps);
        return result;
    }

    /**
     * 菜单权限
     */
    @RequestMapping(value = "/menuListShiro", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> menuListShiro(HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        Map<String, Object> result = new HashMap<String, Object>();

        try {
            param = WebUtils.ReadAsChars(request);
            if (param.get("userId") == null) {
                result.put("code", 9999);
                result.put("msg", "必传参数为空 userId");
                return result;
            }
            List<Permission> maps = menuService.menuListShiro(Long.valueOf(param.get("userId").toString()));
            result.put("code", 10000);
            result.put("data", maps);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 父查子菜单
     */
    @RequestMapping("queryChild")
    public Map<String, Object> queryChildMenu(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();

        try {
            param = WebUtils.ReadAsChars(request);

            if (CheckNullUtils.isNull(String.valueOf(param.get("fatherName")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "fail~");
                return result;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.queryChildMenu(String.valueOf(param.get("fatherName")));
    }

    /**
     * 更换主菜单
     */
    @RequestMapping("changFMenu")
    public Map<String, Object> changFMenu(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        Map<String, Object> saveF = null;
        try {
            param = WebUtils.ReadAsChars(request);

            System.out.println(param);
            saveF = JSONObject.parseObject(String.valueOf(param.get("save")));
            if (CheckNullUtils.isNull(String.valueOf(saveF.get("saveFName"))) || CheckNullUtils.isNull(String.valueOf(saveF.get("saveZId")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "fail~");
                return result;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.changFMenu(String.valueOf(saveF.get("saveFName")), String.valueOf(saveF.get("saveZId")), saveF);
    }

    /**
     * 保存父级菜单
     */
    @RequestMapping("saveFatherMenu")
    public Map<String, Object> saveFatherMenu(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        Map<String, Object> param = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (CheckNullUtils.isNull(String.valueOf(map.get("param")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "保存失败~");
                return result;
            }
            param = JSONObject.parseObject(String.valueOf(map.get("param")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return this.menuService.saveFatherMenu(param);
    }


    /**
     * 保存子级菜单
     */
    @RequestMapping("saveSmallMenu")
    public Map<String, Object> saveSmallMenu(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        Map<String, Object> param = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (CheckNullUtils.isNull(String.valueOf(map.get("smallMenu")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "保存失败~");
                return result;
            }
            param = JSONObject.parseObject(String.valueOf(map.get("smallMenu")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return this.menuService.saveSmallMenu(param);
    }

    /**
     * 修改父级名字
     */
    @RequestMapping("updateFName")
    public Map<String, Object> updateFName(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        Map<String, Object> param = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (CheckNullUtils.isNull(String.valueOf(map.get("updateFName")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "保存失败~");
                return result;
            }
            param = JSONObject.parseObject(String.valueOf(map.get("updateFName")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        return this.menuService.updateFName(param);
    }

    /**
     * 删除子菜单
     */
    @RequestMapping("delSName")
    public Map<String, Object> delSName(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (CheckNullUtils.isNull(String.valueOf(map.get("id")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "保存失败~");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.delSName(String.valueOf(map.get("id")));
    }

    /**
     * 更具id查子菜单
     */
    @RequestMapping("queryZMenuById")
    public Map<String, Object> queryZMenuById(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (CheckNullUtils.isNull(String.valueOf(map.get("id")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "保存失败~");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.queryZMenuById(String.valueOf(map.get("id")));
    }

    /**
     * 根据父id查子菜单
     */
    @RequestMapping("queryZMenuByFId")
    public Map<String, Object> queryZMenuByFId(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (CheckNullUtils.isNull(String.valueOf(map.get("id")))) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "保存失败~");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.queryZMenuByFId(String.valueOf(map.get("id")));
    }


    /**
     * 菜单赋予权限的控制
     */

    @RequestMapping("giveMenuById")
    public Map<String, Object> giveMenuById(HttpServletRequest request) {


        return null;
    }

    /**
     * 根据菜单找到对应的btn
     */
    @RequestMapping("findMenusBtns")
    public Map<String, Object> findMenusBtns(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (map == null) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误~");
                result.put("data", "保存失败~");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.findMenusBtns(map);
    }

    /**
     * 赋予角色按钮
     */
    @RequestMapping("addBtnsShiroToRole")
    public Map<String, Object> addBtnsShiroToRole(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (map == null) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误了~");
                result.put("data", "保存失败~");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.addBtnsShiroToRole(map);
    }

    /**
     * 根据角色查找按钮
     */
    @RequestMapping("queryBtnsByRoleId")
    public Map<String, Object> queryBtnsByRoleId(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = null;
        try {
            map = WebUtils.ReadAsChars(request);
            if (map == null) {
                result.put("code", 5000);
                result.put("msg", "接受参数错误了1~");
                result.put("data", "保存失败~");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.menuService.queryBtnsByRoleId(map);
    }

    /**
     * all in this
     */

    @RequestMapping("allInThis")
    public Map<String, Object> allInThis() {

        return this.menuService.allInThis();
    }

}
